package com.bookshop.servlet;
import com.alibaba.fastjson.JSON;
import com.bookshop.entity.ResponseResult;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置字符编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //获取表单发送的参数
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        ResponseResult result=null;
        if("aiden".equals(username)&&"123456".equals(password)){
            result=new ResponseResult(200,"登录成功");
        }else {
            result=new ResponseResult(400,"登录失败");
        }
        //将对象序列化为json字符串
        String jsonString = JSON.toJSONString(result);
        //响应给客户端信息
        out.print(jsonString);
        //释放资源
        out.flush();
        out.close();
    }
}
